package com.chookss.video.model

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.MyApp
import com.chookss.Urls
import com.chookss.base.StaticClass
import com.chookss.tiku.entity.LiveResult
import com.chookss.tools.httpForm
import com.chookss.tools.MyEvent
import com.chookss.video.entity.VideoCate
import com.chookss.video.entity.VideoComment
import com.chookss.video.entity.VideoEntity
import com.chookss.video.response.*
import com.johnrambo.ktea.base.BaseViewModel
import com.johnrambo.ktea.ktExtends.ShareUtils
import com.johnrambo.ktea.net.http.Method
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/19 8:52
 * Note:
 */
class VideoModel : BaseViewModel() {
    //videoParam	Y	String	搜索参数
//    selectVideoByParm
    fun selectVideoByParm(
        videoParam: String,
        pageNum: Int
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/selectVideoByParm"
            params = mutableMapOf(
                "videoParam" to videoParam,
                "pageNum" to pageNum,
                "pageSize" to 20
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //收藏的视频
    fun getMyItemByType(pageNum: Int): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/sys/my/getMyItemByType"
            method = Method.GET
            params = mutableMapOf(
                "messageType" to 2,
                "pageNum" to pageNum,
                "pageSize" to 20
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //下载分类视频
    fun selectVideoByCatalogCode(
        videoCatalogCode: String,
        pageNum: Int,
        pageSize: Int
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/selectVideoByCatalogCode"
            params = mutableMapOf(
                "videoCatalogCode" to videoCatalogCode,
                "pageNum" to (pageNum + 1).toString(),
                "pageSize" to pageSize.toString()
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //下载分类视频
    fun selectVideoCollectionsCode(
        videoCollectionsCode: String,
        pageNum: Int,
        pageSize: Int
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/selectVideoByCollectionsCode"
            params = mutableMapOf(
                "videoCollectionsCode" to videoCollectionsCode,
                "pageNum" to (pageNum + 1).toString(),
                "pageSize" to pageSize.toString()
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //下载我的分类视频
    fun selectMyVideo(
        employeeCode: String,
        pageNum: Int,
        pageSize: Int
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<VideoListResponse> {
            url = "api/video/getMyVideos"
            params = mutableMapOf(
                "employeeCode" to employeeCode,
                "pageNum" to (pageNum + 1).toString(),
                "pageSize" to pageSize.toString()
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    //下载随即视频
    fun getRankVideo(limit: Int): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/getVideoRand"
            params = mutableMapOf("limit" to limit)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun getVideoRandByFans(pageNum: Int, pageSize: Int): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/getVideoRandByFans"
            params = mutableMapOf(
                "pageNum" to (pageNum + 1).toString(),
                "pageSize" to pageSize.toString()
            )
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun getVideoByCollectionsCode(
        type: String,
        videoCollectionsCode: String,
        employeecode: String
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        if ("1".equals(type)) {
            jobs + httpForm<VideoListResponse> {
                url = "api/video/selectVideoByCollectionsCode"
                params = mutableMapOf("videoCollectionsCode" to videoCollectionsCode)
                onSuccess = {
                    liveResult.postValue(LiveResult(it.data))
                }
                onError = {
                    liveResult.postValue(LiveResult(Error(it)))
                }
            }
        } else if ("3".equals(type)) {
            jobs + httpForm<VideoListResponse> {
                url = "api/video/selectMyVideoByCollectionsCode"
                params = mutableMapOf("videoCollectionsCode" to videoCollectionsCode,
                    "employeeCode" to employeecode)
                onSuccess = {
                    liveResult.postValue(LiveResult(it.data))
                }
                onError = {
                    liveResult.postValue(LiveResult(Error(it)))
                }
            }
        } else {
            jobs + httpForm<VideoListResponse> {
                url = "api/video/selectVideoByCatalogCode"
                params = mutableMapOf("videoCatalogCode" to videoCollectionsCode)
                onSuccess = {
                    liveResult.postValue(LiveResult(it.data))
                }
                onError = {
                    liveResult.postValue(LiveResult(Error(it)))
                }
            }
        }

        return liveResult
    }

    fun getVideoByNewCollectionsCode(
        type: String,
        effTime: String,
        invalidTime: String,
        videoCollectionsCode: String
    ): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<VideoListResponse> {
            url =Urls.selectTaskByCollectionsCode
            method = Method.GET
            params = mutableMapOf("videoCollectionsCode" to videoCollectionsCode,
                "effTime" to effTime,
                "invalidTime" to invalidTime)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun getVideoPlayAuth(videoId: String): LiveData<LiveResult<String>> {
        var liveResult = MutableLiveData<LiveResult<String>>()

        jobs + httpForm<GetVideoPlayAuthResponse> {
            url = "api/video/getVideoPlayAuth"
            params = mutableMapOf("videoId" to videoId)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data.playAuth))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

//    employeeCode	Y	String	视频上传者的员工编码
//    employeeName	Y	String	视频上传者的员工名称
//    employeeId	Y	String	视频上传者的员工id
//    videoTitle	Y	String	视频标题
//    videoId	Y	String	视频地址
//    isLike	Y	String	点赞（1：点赞，-1取消点赞）
//    videoCoverPath	N	String	视频默认封面地址
//    likeType	Y	String	点赞类型：0：视频 1：评论

    fun likeVideo(
        videoId: String,
        messageTitle: String,
        employeeCode: String,
        isLike: Boolean
    ): LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
//        var employeeCode = ShareUtils().getString(MyApp.appCtx, StaticClass.EMPLOYEECODE, "")
        val params2: MutableMap<String, Any?> = mutableMapOf(
            "likeId" to videoId,
            "employeeCode" to employeeCode,
            "messageTitle" to messageTitle,
            "isLike" to (if (isLike) "1" else "-1"),
            "likeType" to "0"
        )
        jobs + httpForm<GetVideoPlayAuthResponse> {
            url = "api/video/likeVideo"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(null))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun shareVideo(videoId: String): LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
        var employeeCode = ShareUtils().getString(MyApp.appCtx, StaticClass.EMPLOYEECODE, "")
        val params2: MutableMap<String, Any?> = mutableMapOf(
            "shareCode" to videoId,
            "employeeCode" to employeeCode,
        )
        jobs + httpForm<GetVideoPlayAuthResponse> {
            url = "api/video/shareVideo"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(null))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun collectVideo(videoId: String, isCollect: Boolean): LiveData<LiveResult<Any>> {
        var liveResult = MutableLiveData<LiveResult<Any>>()
        val params2: MutableMap<String, Any?> = mutableMapOf(
            "collectId" to videoId,
            "collectStatus" to (if (isCollect) "1" else "0"),
            "collectType" to "0"
        )
        jobs + httpForm<GetVideoPlayAuthResponse> {
            url = "api/sys/my/toCollect"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(null))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

    fun likeComment(
        videoId: String,
        messageTitle: String,
        isLike: Boolean,
        _onSuccess: (() -> Unit)?,
        _onError: ((String) -> Unit)?
    ) {
        var employeeCode = ShareUtils().getString(MyApp.appCtx, StaticClass.EMPLOYEECODE, "")
        val params2: MutableMap<String, Any?> = mutableMapOf(
            "likeId" to videoId,
            "employeeCode" to employeeCode,
            "messageTitle" to messageTitle,
            "isLike" to (if (isLike) "1" else "-1"),
            "likeType" to "1"
        )
        jobs + httpForm<GetVideoPlayAuthResponse> {
            url = "api/video/likeVideo"
            params = params2
            onSuccess = {
                _onSuccess?.invoke()
            }
            onError = {
                _onError?.invoke(it)
            }
        }
    }

    //selectCommentByCode
    fun getVideoComments(
        code: String,
        pageSize: Int,
        pageNum: Int
    ): LiveData<LiveResult<List<VideoComment>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoComment>>>()

        val params2: MutableMap<String, Any?> = HashMap()
        params2["code"] = code
        params2["pageNum"] = (pageNum + 1).toString()
        params2["pageSize"] = pageSize.toString()

        jobs + httpForm<VideoCommentsListResponse> {
            url = "api/video/selectCommentByCode"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    //评论
    fun commentVideo(
        video: VideoEntity,
        comment: String,
        _onSuccess: ((String) -> Unit)?,
        _onError: ((String) -> Unit)?
    ) {
//        employeeCode	Y	String	视频上传者的员工编码
//        employeeName	Y	String	视频上传者的员工名称
//        parentCode	Y	String	被回复的评论的id
//        commentId	Y	String	视频编码、新闻编码、评论编码
//        videoComment	N	String	评论内容
//        commentType	Y	String	评论类型：1：视频评论 2：评论的回复 3：新闻的评论
//        videoCoverPath	N	String	如果时评论的视频需要传此参数y
        var employeeCode = ShareUtils().getString(MyApp.appCtx, StaticClass.EMPLOYEECODE, "")

        val params2: MutableMap<String, Any?> = mutableMapOf(
            "employeeCode" to video.employeeCode,
            "messageTitle" to video.videoTitle,
            "videoComment" to comment,
            "videoCoverPath" to video.videoCoverPath,
            "commentId" to video.videoId,
            "commentType" to "1",
            "lvlNo" to "1",
        )

        jobs + httpForm<VideoCommentResponse> {
            url = "api/video/commentVideo"
            params = params2
            onSuccess = {
                val reObject = JSONObject(it.data)
                var map: HashMap<String, String> = HashMap()
                map.put("videoId", video.videoId)
                map.put("totalCommentNum", reObject.getString("totalCommentNum"))
                EventBus.getDefault().post(MyEvent(map))
                println(video.videoId + "---")
                _onSuccess?.invoke(reObject.getString("commentCode"))
            }
            onError = {
                _onError?.invoke(it)
            }
        }
    }

    //    对video评论
//    commentId=视频编码
//    parentcode=null
//    commentType=1
//    lvlNo=1
//
//    对评论回复
//    commentId=评论编码
//    parentcode=和commentId值一样
//    commentType=2
//    lvlNo=2
//
//    对回复的回复
//    commentId=评论编码
//    parentcode=和commentId值一样
//    commentType=2
//    lvlNo=3
    //评论
    fun commentComment(
        employeeCode: String,
        messageTitle: String,
        commendId: String,
        parentCode: String?,
        videoCoverPath: String,
        comment: String,
        _onSuccess: ((String) -> Unit)?,
        _onError: ((String) -> Unit)?
    ) {
        val params2: MutableMap<String, Any?> = mutableMapOf(
            "employeeCode" to employeeCode,
            "messageTitle" to messageTitle,
            "videoComment" to comment,
            "commentId" to commendId,
            "videoCoverPath" to videoCoverPath,
            "commentType" to "2"
        )
        if (!TextUtils.isEmpty(parentCode)) {
            params2["parentCode"] = commendId
            params2["lvlNo"] = "3"
        } else {
            params2["parentCode"] = commendId
            params2["lvlNo"] = "2"
        }

        jobs + httpForm<VideoCommentResponse> {
            url = "api/video/commentVideo"
            params = params2
            onSuccess = {
                val reObject = JSONObject(it.data)
                _onSuccess?.invoke(reObject.getString("commentCode"))
            }
            onError = {
                _onError?.invoke(it)
            }
        }
    }

    //selectAppVideoCatalogTree
    fun selectAppVideoCatalogTree(
        code: String?,
        videoType: String,
        parentLvl: String = "0"
    ): LiveData<LiveResult<List<VideoCate>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoCate>>>()

        val params2: MutableMap<String, Any?> = HashMap()
        params2["parentLvl"] = "0"

        code?.let { params2["code"] = it }
        params2["parentLvl"] = parentLvl
        params2["videoType"] = videoType
        params2["catalogType"] = videoType
        jobs + httpForm<VideoCateListResponse> {
            url = "api/video/selectAppVideoCatalogTree"
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    //selectRecommend
//    searchType	Y	String	1-推荐 2热点 3 明星
    fun selectRecommend(searchType: String): LiveData<LiveResult<List<VideoEntity>>> {
        var liveResult = MutableLiveData<LiveResult<List<VideoEntity>>>()
        jobs + httpForm<RandVideoResponse> {
            url = "api/video/selectRecommend"
            params = mutableMapOf("searchType" to searchType)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }
}